#include <iostream>
#include <iomanip>
#include <ctime>
#include <windows.h>
#include <vector>
#include <string>
#include <cmath>
#include <stdexcept>

// 用于农历转公历计算的数组（简化版）
const int lunarInfo[1901 - 1900 + 2099 - 1901 + 1] = {
    0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,
    0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977,
    0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970,
    0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950,
    0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557,
    0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0,
    0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0,
    0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6,
    0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570,
    0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0,
    0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5,
    0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930,
    0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530,
    0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45,
    0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0
};

// 农历数据
struct LunarData {
    int year;
    int month;
    int day;
    bool isLeapMonth;
    std::string lunarYearName;
    std::string lunarMonthName;
    std::string lunarDayName;
};

// 节气数据
struct SolarTerm {
    std::string name;
    int month;
    int day;
};

// 节日数据
struct Holiday {
    std::string name;
    int month;
    int day;
    bool isLunar; // 是否为农历节日
};

// 函数声明
void setConsoleColor(int color);
void resetConsoleColor();
void displayMainMenu();
void queryLunarDate();
void queryGregorianDate();
void displayMonthCalendar(int year, int month);
void calculateDaysFromToday();
void calculateDateFromDays();
void calculateDaysBetweenDates();
void displaySolarTerms(int year);
void displayHolidays(int year);


LunarData solarToLunar(int year, int month, int day);
std::vector<int> lunarToSolar(int lunarYear, int lunarMonth, int lunarDay, bool isLeapMonth);
bool isLeapYear(int year);
int getDaysInMonth(int year, int month);
int getDayOfWeek(int year, int month, int day);
std::vector<SolarTerm> getSolarTerms(int year);
std::vector<Holiday> getHolidays();
bool isValidDate(int year, int month, int day);
int getLeapMonth(int lunarYear);
int getLunarMonthDays(int lunarYear, int lunarMonth, bool isLeap);
int getLunarYearDays(int lunarYear);

// 主函数
int main() {
    setConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // 设置控制台颜色为白色
    while (true) {
        system("cls");
        displayMainMenu();
        int choice;
        std::cout << "请输入选项 (0退出): ";
        while (!(std::cin >> choice) || choice < 0 || choice > 8) {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "无效输入，请重新输入选项 (0 - 8): ";
        }
        std::cin.ignore(); // 忽略输入缓冲区的换行符

        try {
            switch (choice) {
            case 0:
                std::cout << "感谢使用超级万年历！再见！\n";
                return 0;
            case 1:
                queryLunarDate();
                break;
            case 2:
                queryGregorianDate();
                break;
            case 3: {
                int year, month;
                std::cout << "请输入年份和月份 (格式: yyyy mm): ";
                while (!(std::cin >> year >> month) || !isValidDate(year, month, 1)) {
                    std::cin.clear();
                    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                    std::cout << "无效输入，请重新输入年份和月份 (格式: yyyy mm): ";
                }
                std::cin.ignore();
                displayMonthCalendar(year, month);
                break;
            }
            case 4:
                calculateDaysFromToday();
                break;
            case 5:
                calculateDateFromDays();
                break;
            case 6:
                calculateDaysBetweenDates();
                break;
            case 7: {
                int year;
                std::cout << "请输入年份: ";
                while (!(std::cin >> year) || year < 1900 || year > 2100) {
                    std::cin.clear();
                    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                    std::cout << "无效输入，请重新输入年份 (1900-2100): ";
                }
                std::cin.ignore();
                displaySolarTerms(year);
                break;
            }
            case 8: {
                int year;
                std::cout << "请输入年份: ";
                while (!(std::cin >> year) || year < 1900 || year > 2100) {
                    std::cin.clear();
                    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                    std::cout << "无效输入，请重新输入年份 (1900-2100): ";
                }
                std::cin.ignore();
                displayHolidays(year);
                break;
            }
            default:
                std::cout << "无效的选项，请重新输入。\n";
            }
        } catch (const std::exception& e) {
            setConsoleColor(FOREGROUND_RED);
            std::cout << "错误: " << e.what() << "\n";
            resetConsoleColor();
        }

        std::cout << "\n按Enter键返回主菜单...";
        std::cin.get();
    }
    return 0;
}

// 设置控制台颜色
void setConsoleColor(int color) {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, color);
}

// 重置控制台颜色
void resetConsoleColor() {
    setConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}

// 显示主菜单
void displayMainMenu() {
    time_t now = time(nullptr);
    struct tm* currentTime = localtime(&now);
    char timeStr[26];
    strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", currentTime);

    setConsoleColor(FOREGROUND_RED);
    std::cout << "当前时间: " << timeStr << std::endl;
    resetConsoleColor();

    std::cout << "==================== 超级万年历 ====================\n";
    std::cout << "1. 查询农历\n";
    std::cout << "2. 查询公历\n";
    std::cout << "3. 显示月历\n";
    std::cout << "4. 查询某天距今天的天数\n";
    std::cout << "5. 查询距今天相应天数的日期\n";
    std::cout << "6. 查询任意两天之间的天数差\n";
    std::cout << "7. 显示二十四节气\n";
    std::cout << "8. 显示节日\n";
    std::cout << "0. 退出\n";
    std::cout << "===================================================\n";
}

// 查询农历
void queryLunarDate() {
    int year, month, day;
    std::cout << "请输入公历日期 (格式: yyyy mm dd): ";
    while (!(std::cin >> year >> month >> day) || !isValidDate(year, month, day)) {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "无效输入，请重新输入公历日期 (格式: yyyy mm dd): ";
    }
    std::cin.ignore();

    LunarData lunar = solarToLunar(year, month, day);

    setConsoleColor(FOREGROUND_GREEN);
    std::cout << "\n公历: " << year << "年" << month << "月" << day << "日\n";
    std::cout << "农历: " << lunar.lunarYearName << "年 ";
    if (lunar.isLeapMonth) {
        std::cout << "闰";
    }
    std::cout << lunar.lunarMonthName << "月" << lunar.lunarDayName << "\n";
    resetConsoleColor();
}

// 查询公历
void queryGregorianDate() {
    std::string lunarYearName;
    int lunarYear, lunarMonth, lunarDay;
    bool isLeapMonth;

    std::cout << "请输入农历信息:\n";
    
    // 获取农历年份
    std::cout << "生肖年份 (如: 甲子年): ";
    std::getline(std::cin, lunarYearName);
    
    // 解析农历年份为数字
    if (lunarYearName.length() != 3 || lunarYearName.substr(2, 1) != "年") {
        throw std::invalid_argument("农历年份格式错误，请输入如'甲子年'的格式");
    }
    
    // 简化的干支转换
    const std::string celestialStems[] = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
    const std::string earthlyBranches[] = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
    
    int stemIndex = -1, branchIndex = -1;
    for (int i = 0; i < 10; i++) {
        if (lunarYearName.substr(0, 1) == celestialStems[i]) {
            stemIndex = i;
            break;
        }
    }
    
    for (int i = 0; i < 12; i++) {
        if (lunarYearName.substr(1, 1) == earthlyBranches[i]) {
            branchIndex = i;
            break;
        }
    }
    
    if (stemIndex == -1 || branchIndex == -1) {
        throw std::invalid_argument("无法识别的干支年份");
    }
    
    // 计算对应的公历年 (简化版，实际应使用更精确的算法)
    lunarYear = 1900 + (stemIndex - 4 + 10) % 10 + ((branchIndex - 4 + 12) % 12) * 10;
    if (lunarYear < 1900) lunarYear += 60; // 60年一循环
    
    // 获取是否闰月
    std::cout << "是否闰月 (1是/0否): ";
    while (!(std::cin >> isLeapMonth) || (isLeapMonth != 0 && isLeapMonth != 1)) {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "无效输入，请输入0或1: ";
    }
    std::cin.ignore();
    
    // 获取农历月份
    std::cout << "月份 (1-12): ";
    while (!(std::cin >> lunarMonth) || lunarMonth < 1 || lunarMonth > 12) {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "无效输入，请输入1到12之间的数字: ";
    }
    std::cin.ignore();
    
    // 获取农历日期
    std::cout << "日期 (1-30): ";
    while (!(std::cin >> lunarDay) || lunarDay < 1 || lunarDay > 30) {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "无效输入，请输入1到30之间的数字: ";
    }
    std::cin.ignore();
    
    // 检查闰月有效性
    if (isLeapMonth && getLeapMonth(lunarYear) != lunarMonth) {
        throw std::invalid_argument("该年没有闰" + std::to_string(lunarMonth) + "月");
    }
    
    // 检查日期有效性
    if (lunarDay > getLunarMonthDays(lunarYear, lunarMonth, isLeapMonth)) {
        throw std::invalid_argument("该月没有" + std::to_string(lunarDay) + "日");
    }
    
    // 农历转公历
    std::vector<int> solar = lunarToSolar(lunarYear, lunarMonth, lunarDay, isLeapMonth);
    if (solar[0] == -1) {
        throw std::runtime_error("农历转公历计算失败");
    }
    
    setConsoleColor(FOREGROUND_GREEN);
    std::cout << "\n农历: " << lunarYearName << "年 ";
    if (isLeapMonth) {
        std::cout << "闰";
    }
    std::cout << lunarMonth << "月" << lunarDay << "日\n";
    std::cout << "公历: " << solar[0] << "年" << solar[1] << "月" << solar[2] << "日\n";
    resetConsoleColor();
}

// 显示月历
void displayMonthCalendar(int year, int month) {
    if (!isValidDate(year, month, 1)) {
        std::cout << "输入的日期范围无效！\n";
        return;
    }

    setConsoleColor(FOREGROUND_BLUE);
    std::cout << "\n" << std::setw(20) << std::right << year << "年" << month << "月\n";
    resetConsoleColor();

    std::cout << "  日  一  二  三  四  五  六\n";

    // 获取该月第一天是星期几
    int firstDayOfWeek = getDayOfWeek(year, month, 1);

    // 获取该月的天数
    int daysInMonth = getDaysInMonth(year, month);

    // 打印空格占位
    for (int i = 0; i < firstDayOfWeek; i++) {
        std::cout << "    ";
    }

    // 打印日期
    for (int day = 1; day <= daysInMonth; day++) {
        // 获取农历信息
        LunarData lunar = solarToLunar(year, month, day);

        // 周末显示为红色
        if ((firstDayOfWeek + day) % 7 == 0 || (firstDayOfWeek + day - 1) % 7 == 0) {
            setConsoleColor(FOREGROUND_RED);
        }

        // 节日显示为绿色
        bool isHoliday = false;
        std::vector<Holiday> holidays = getHolidays();
        for (const auto& holiday : holidays) {
            if (!holiday.isLunar && holiday.month == month && holiday.day == day) {
                setConsoleColor(FOREGROUND_GREEN);
                isHoliday = true;
                break;
            }
        }

        std::cout << std::setw(4) << day;
        resetConsoleColor();

        // 每个月的第一天显示农历月份
        if (day == 1) {
            std::cout << lunar.lunarMonthName;
        }
        // 农历初一显示"初一"
        else if (lunar.lunarDayName == "初一") {
            std::cout << lunar.lunarDayName;
        }
        // 节气显示节气名称
        else {
            std::vector<SolarTerm> terms = getSolarTerms(year);
            for (const auto& term : terms) {
                if (term.month == month && term.day == day) {
                    std::cout << term.name;
                    break;
                }
            }
        }

        // 换行
        if ((firstDayOfWeek + day) % 7 == 0) {
            std::cout << "\n";
        }
    }
    std::cout << "\n";
}

// 计算某天距今天的天数
void calculateDaysFromToday() {
    time_t now = time(nullptr);
    struct tm* currentTime = localtime(&now);
    
    int targetYear, targetMonth, targetDay;
    std::cout << "请输入目标日期 (格式: yyyy mm dd): ";
    while (!(std::cin >> targetYear >> targetMonth >> targetDay) || !isValidDate(targetYear, targetMonth, targetDay)) {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "无效输入，请重新输入目标日期 (格式: yyyy mm dd): ";
    }
    std::cin.ignore();
    
    // 构建目标日期的tm结构
    struct tm targetTime = {0};
    targetTime.tm_year = targetYear - 1900;
    targetTime.tm_mon = targetMonth - 1;
    targetTime.tm_mday = targetDay;
    targetTime.tm_hour = 12; // 设置为中午以避免夏令时问题
    
    // 转换为时间戳
    time_t targetTimestamp = mktime(&targetTime);
    
    // 计算天数差
    double secondsDiff = difftime(targetTimestamp, now);
    int daysDiff = static_cast<int>(secondsDiff / (60 * 60 * 24));
    
    setConsoleColor(FOREGROUND_GREEN);
    std::cout << "今天: " << currentTime->tm_year + 1900 << "年" 
              << currentTime->tm_mon + 1 << "月" << currentTime->tm_mday << "日\n";
    std::cout << "目标日期: " << targetYear << "年" << targetMonth << "月" << targetDay << "日\n";
    std::cout << "相差天数: " << daysDiff << "天\n";
    resetConsoleColor();
}

// 计算距今天相应天数的日期
void calculateDateFromDays() {
    int days;
    std::cout << "请输入天数 (正数表示未来，负数表示过去): ";
    while (!(std::cin >> days)) {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "无效输入，请输入整数: ";
    }
    std::cin.ignore();
    
    time_t now = time(nullptr);
    time_t targetTime = now + days * 24 * 60 * 60;
    
    struct tm* targetTm = localtime(&targetTime);
    
    LunarData lunar = solarToLunar(targetTm->tm_year + 1900, targetTm->tm_mon + 1, targetTm->tm_mday);
    
    setConsoleColor(FOREGROUND_GREEN);
    std::cout << "今天: " << targetTm->tm_year + 1900 - days/365 << "年" 
              << targetTm->tm_mon + 1 << "月" << targetTm->tm_mday << "日\n";
    std::cout << days << "天后的日期: " 
              << targetTm->tm_year + 1900 << "年" 
              << targetTm->tm_mon + 1 << "月" 
              << targetTm->tm_mday << "日\n";
    std::cout << "农历: " << lunar.lunarYearName << "年 ";
    if (lunar.isLeapMonth) {
        std::cout << "闰";
    }
    std::cout << lunar.lunarMonthName << "月" << lunar.lunarDayName << "\n";
    resetConsoleColor();
}

// 计算任意两天之间的天数差
void calculateDaysBetweenDates() {
    int year1, month1, day1;
    int year2, month2, day2;
    
    std::cout << "请输入第一个日期 (格式: yyyy mm dd): ";
    while (!(std::cin >> year1 >> month1 >> day1) || !isValidDate(year1, month1, day1)) {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "无效输入，请重新输入第一个日期 (格式: yyyy mm dd): ";
    }
    std::cin.ignore();
    
    std::cout << "请输入第二个日期 (格式: yyyy mm dd): ";
    while (!(std::cin >> year2 >> month2 >> day2) || !isValidDate(year2, month2, day2)) {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "无效输入，请重新输入第二个日期 (格式: yyyy mm dd): ";
    }
    std::cin.ignore();
    
    // 构建两个日期的tm结构
    struct tm time1 = {0};
    time1.tm_year = year1 - 1900;
    time1.tm_mon = month1 - 1;
    time1.tm_mday = day1;
    time1.tm_hour = 12;
    
    struct tm time2 = {0};
    time2.tm_year = year2 - 1900;
    time2.tm_mon = month2 - 1;
    time2.tm_mday = day2;
    time2.tm_hour = 12;
    
    // 转换为时间戳
    time_t timestamp1 = mktime(&time1);
    time_t timestamp2 = mktime(&time2);
    
    // 计算天数差
    double secondsDiff = difftime(timestamp2, timestamp1);
    int daysDiff = static_cast<int>(secondsDiff / (60 * 60 * 24));
    
    setConsoleColor(FOREGROUND_GREEN);
    std::cout << "第一个日期: " << year1 << "年" << month1 << "月" << day1 << "日\n";
    std::cout << "第二个日期: " << year2 << "年" << month2 << "月" << day2 << "日\n";
    std::cout << "相差天数: " << std::abs(daysDiff) << "天\n";
    if (daysDiff > 0) {
        std::cout << "第二个日期比第一个日期晚 " << daysDiff << " 天\n";
    } else if (daysDiff < 0) {
        std::cout << "第二个日期比第一个日期早 " << -daysDiff << " 天\n";
    } else {
        std::cout << "两个日期相同\n";
    }
    resetConsoleColor();
}

// 显示二十四节气
void displaySolarTerms(int year) {
    setConsoleColor(FOREGROUND_BLUE);
    std::cout << "\n" << year << "年二十四节气:\n";
    resetConsoleColor();
    
    std::vector<SolarTerm> terms = getSolarTerms(year);
    
    for (size_t i = 0; i < terms.size(); i++) {
        std::cout << std::setw(4) << terms[i].name << ": " 
                  << year << "年" << terms[i].month << "月" << terms[i].day << "日";
        
        // 每行显示两个节气
        if ((i + 1) % 2 == 0) {
            std::cout << "\n";
        } else {
            std::cout << "    ";
        }
    }
    std::cout << "\n";
}

// 显示节日
void displayHolidays(int year) {
    setConsoleColor(FOREGROUND_BLUE);
    std::cout << "\n" << year << "年节日:\n";
    resetConsoleColor();
    
    std::vector<Holiday> holidays = getHolidays();
    
    for (const auto& holiday : holidays) {
        if (holiday.isLunar) {
            // 农历节日需要转换为公历
            std::vector<int> solar = lunarToSolar(year, holiday.month, holiday.day, false);
            if (solar[0] != -1) {
                std::cout << holiday.name << " (农历" << holiday.month << "月" << holiday.day << "日，公历" 
                          << solar[0] << "年" << solar[1] << "月" << solar[2] << "日)\n";
            } else {
                std::cout << holiday.name << " (农历" << holiday.month << "月" << holiday.day << "日)\n";
            }
        } else {
            std::cout << holiday.name << " (公历" << year << "年" << holiday.month << "月" << holiday.day << "日)\n";
        }
    }
    std::cout << "\n";
}

// 判断是否为闰年
bool isLeapYear(int year) {
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

// 获取指定月份的天数
int getDaysInMonth(int year, int month) {
    static const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (month == 2 && isLeapYear(year)) {
        return 29;
    }
    return days[month - 1];
}

// 获取指定日期是星期几 (0-6, 0表示星期日)
int getDayOfWeek(int year, int month, int day) {
    if (month < 3) {
        month += 12;
        year--;
    }
    int c = year / 100;
    year %= 100;
    int w = (c / 4 - 2 * c + year + year / 4 + 13 * (month + 1) / 5 + day - 1) % 7;
    return (w + 7) % 7;
}

// 获取农历闰月
int getLeapMonth(int lunarYear) {
    return (lunarInfo[lunarYear - 1900] & 0xf);
}

// 获取农历月份的天数
int getLunarMonthDays(int lunarYear, int lunarMonth, bool isLeap) {
    if (isLeap && lunarMonth != getLeapMonth(lunarYear)) {
        return 0; // 不是闰月
    }
    
    if (isLeap) {
        return (lunarInfo[lunarYear - 1900] & (0x10000 >> lunarMonth)) ? 30 : 29;
    } else {
        return (lunarInfo[lunarYear - 1900] & (0x10000 >> lunarMonth)) ? 30 : 29;
    }
}

// 获取农历年的总天数
int getLunarYearDays(int lunarYear) {
    int days = 0;
    int leapMonth = getLeapMonth(lunarYear);
    
    // 计算每个月的天数
    for (int i = 1; i <= 12; i++) {
        days += getLunarMonthDays(lunarYear, i, false);
    }
    
    // 如果有闰月，加上闰月的天数
    if (leapMonth > 0) {
        days += getLunarMonthDays(lunarYear, leapMonth, true);
    }
    
    return days;
}

// 公历转农历
LunarData solarToLunar(int year, int month, int day) {
    LunarData lunar;
    lunar.year = year;
    lunar.month = month;
    lunar.day = day;
    lunar.isLeapMonth = false;
    
    // 简化版农历年份名称
    const std::string celestialStems[] = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
    const std::string earthlyBranches[] = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
    const std::string zodiacAnimals[] = {"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};
    
    // 计算干支纪年
    int stemIndex = (year - 4) % 10;
    int branchIndex = (year - 4) % 12;
    lunar.lunarYearName = celestialStems[stemIndex] + earthlyBranches[branchIndex] + "年(" + zodiacAnimals[branchIndex] + ")";
    
    // 农历月份名称
    const std::string lunarMonthNames[] = {"正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"};
    
    // 这里使用简化算法，实际应该使用更精确的农历计算方法
    // 以下是一个简化的农历计算，实际应用中应该使用更复杂的算法
    
    // 农历日期名称
    if (day == 1) {
        lunar.lunarDayName = "初一";
    } else if (day == 2) {
        lunar.lunarDayName = "初二";
    } else if (day == 3) {
        lunar.lunarDayName = "初三";
    } else if (day == 10) {
        lunar.lunarDayName = "初十";
    } else if (day == 20) {
        lunar.lunarDayName = "二十";
    } else if (day == 30) {
        lunar.lunarDayName = "三十";
    } else if (day < 10) {
        lunar.lunarDayName = "初" + std::to_string(day);
    } else if (day < 20) {
        lunar.lunarDayName = "十" + std::to_string(day - 10);
    } else if (day < 30) {
        lunar.lunarDayName = "二十" + std::to_string(day - 20);
    } else {
        lunar.lunarDayName = "三十";
    }
    
    // 简化处理：直接使用公历月份对应的农历月份名称
    lunar.lunarMonthName = lunarMonthNames[month - 1];
    
    return lunar;
}

// 农历转公历 (简化版)
std::vector<int> lunarToSolar(int lunarYear, int lunarMonth, int lunarDay, bool isLeapMonth) {
    std::vector<int> result = {-1, -1, -1}; // 默认返回错误值
    
    // 检查年份范围
    if (lunarYear < 1900 || lunarYear > 2099) {
        return result;
    }
    
    // 检查闰月是否正确
    int leapMonth = getLeapMonth(lunarYear);
    if (isLeapMonth && lunarMonth != leapMonth) {
        return result;
    }
    
    // 计算从1900年1月31日开始的天数
    int days = 0;
    // 计算到目标年前的总天数
    for (int y = 1900; y < lunarYear; y++) {
        days += getLunarYearDays(y);
    }
    
    // 计算到目标月前的总天数
    for (int m = 1; m < lunarMonth; m++) {
        days += getLunarMonthDays(lunarYear, m, false);
    }
    
    // 如果是闰月，加上闰月前的天数
    if (isLeapMonth) {
        days += getLunarMonthDays(lunarYear, lunarMonth, true);
    }
    
    // 加上目标月的天数
    days += lunarDay;
    
    // 1900年1月31日是农历1900年正月初一
    // 对应的公历是1900年1月31日
    // 计算公历日期
    struct tm baseDate = {0};
    baseDate.tm_year = 1900 - 1900;
    baseDate.tm_mon = 0;
    baseDate.tm_mday = 31;
    baseDate.tm_hour = 12;
    
    time_t baseTime = mktime(&baseDate);
    time_t targetTime = baseTime + days * 24 * 60 * 60;
    
    struct tm* targetTm = localtime(&targetTime);
    
    result[0] = targetTm->tm_year + 1900;
    result[1] = targetTm->tm_mon + 1;
    result[2] = targetTm->tm_mday;
    
    return result;
}

// 检查日期是否有效
bool isValidDate(int year, int month, int day) {
    if (year < 1900 || year > 2100 || month < 1 || month > 12 || day < 1) {
        return false;
    }
    
    int daysInMonth = getDaysInMonth(year, month);
    return day <= daysInMonth;
}

// 获取指定年份的二十四节气 (修复版)
std::vector<SolarTerm> getSolarTerms(int year) {
    std::vector<SolarTerm> terms;
    
    // 节气计算公式（简化版）
    // 注：真实计算需要使用天文算法，这里仅为示例
    const char* termNames[] = {
        "立春", "雨水", "惊蛰", "春分", "清明", "谷雨",
        "立夏", "小满", "芒种", "夏至", "小暑", "大暑",
        "立秋", "处暑", "白露", "秋分", "寒露", "霜降",
        "立冬", "小雪", "大雪", "冬至", "小寒", "大寒"
    };
    
    // 节气日期基准值（2000年为基准）
    const double baseDays[24] = {
        4.6295, 19.4599, 5.636, 20.646, 4.81, 20.12,
        5.52, 21.04, 5.678, 21.37, 7.108, 22.83,
        7.928, 23.65, 7.646, 23.042, 8.318, 23.438,
        7.438, 22.36, 7.18, 21.94, 5.4055, 20.12
    };
    
    // 每年的修正值（简化版）
    double correction = (year - 2000) * 0.2422;
    
    for (int i = 0; i < 24; i++) {
        SolarTerm term;
        term.name = termNames[i];
        
        // 计算月份
        int month = i / 2 + 1;
        
        // 计算日期（修正类型转换问题）
        double exactDay = baseDays[i] + correction;
        int leapDays = 0;
        
        // 处理闰年的特殊情况（简化处理）
        if (i < 2 && isLeapYear(year)) {
            leapDays = 1;
        }
        
        int day = static_cast<int>(std::round(exactDay)) - leapDays;
        
        // 检查日期是否合理
        if (day < 1) day = 1;
        if (day > getDaysInMonth(year, month)) {
            day = getDaysInMonth(year, month);
        }
        
        term.month = month;
        term.day = day;
        terms.push_back(term);
    }
    
    return terms;
}

// 获取节日数据
std::vector<Holiday> getHolidays() {
    std::vector<Holiday> holidays;
    
    // 公历节日
    holidays.push_back({"元旦", 1, 1, false});
    holidays.push_back({"情人节", 2, 14, false});
    holidays.push_back({"妇女节", 3, 8, false});
    holidays.push_back({"植树节", 3, 12, false});
    holidays.push_back({"愚人节", 4, 1, false});
    holidays.push_back({"劳动节", 5, 1, false});
    holidays.push_back({"青年节", 5, 4, false});
    holidays.push_back({"母亲节", 5, 10, false}); // 示例日期，实际需计算
    holidays.push_back({"儿童节", 6, 1, false});
    holidays.push_back({"父亲节", 6, 17, false}); // 示例日期，实际需计算
    holidays.push_back({"建党节", 7, 1, false});
    holidays.push_back({"建军节", 8, 1, false});
    holidays.push_back({"教师节", 9, 10, false});
    holidays.push_back({"国庆节", 10, 1, false});
    holidays.push_back({"万圣节", 11, 1, false});
    holidays.push_back({"感恩节", 11, 22, false}); // 示例日期，实际需计算
    holidays.push_back({"圣诞节", 12, 25, false});
    
    // 农历节日
    holidays.push_back({"春节", 1, 1, true});
    holidays.push_back({"元宵节", 1, 15, true});
    holidays.push_back({"清明节", 4, 5, false}); // 清明既是节气也是节日
    holidays.push_back({"端午节", 5, 5, true});
    holidays.push_back({"七夕节", 7, 7, true});
    holidays.push_back({"中元节", 7, 15, true});
    holidays.push_back({"中秋节", 8, 15, true});
    holidays.push_back({"重阳节", 9, 9, true});
    holidays.push_back({"冬至", 12, 22, false}); // 冬至既是节气也是节日
    holidays.push_back({"腊八节", 12, 8, true});
    holidays.push_back({"小年", 12, 23, true});
    
    return holidays;
}    